﻿using System;
using System.Xml.Serialization;
using System.Web.Configuration;

namespace Xmlt.Sample
{
    [Serializable]
    public class MailMessage : System.Net.Mail.MailMessage
    {

        public new string ReplyTo
        {
            get
            {
                return base.ReplyTo.ToString();
            }
            set
            {
                base.ReplyTo = new System.Net.Mail.MailAddress(value);
            }
        }

        public new string Sender
        {
            get
            {
                return base.Sender.ToString();
            }
            set
            {
                base.Sender = new System.Net.Mail.MailAddress(value);
            }
        }

        public new string To
        {
            get
            {
                return base.To.ToString();
            }
            set
            {
                base.To.Add(value);
            }
        }

        public new string From
        {
            get
            {
                return base.From.Address;
            }
            set
            {
                base.From = new System.Net.Mail.MailAddress(value);
            }
        }
    }

    internal class MailHelper
    {
        public static void SendEmail(string message)
        {
            
            XmlAttributeOverrides overrides = new XmlAttributeOverrides();
            XmlAttributes attributes = new XmlAttributes { XmlIgnore = true };
            overrides.Add(typeof(System.Net.Mail.MailMessage), "To", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "Bcc", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "CC", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "AlternateViews", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "Attachments", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "Headers", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "ReplyTo", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "Sender", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), "From", attributes);
            overrides.Add(typeof(System.Net.Mail.MailMessage), new XmlAttributes { XmlType = new XmlTypeAttribute { TypeName = "InnerMailMessage" } });
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(MailMessage), overrides);
            MailMessage mailMessage = xmlSerializer.Deserialize(new System.IO.StringReader(message)) as MailMessage;

            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(WebConfigurationManager.AppSettings["smtpServer"]);
            smtp.Credentials = new System.Net.NetworkCredential(WebConfigurationManager.AppSettings["smtpUsername"], WebConfigurationManager.AppSettings["smtpPassword"]);
            smtp.Send(mailMessage);
            
        }
    }
}
